
python = find_program('python3')
proto_includes_link = custom_target(
  'proto_includes_link',
  output: 'include',  # directory adjacent to protoc binary
  command: [
    python,
    files('symlink_or_copy.py'),
    meson.current_source_dir(),
    '@OUTPUT@',
  ],
)
proto_includes_link_dep = declare_dependency(
  sources: proto_includes_link,
)

absl_log_initialize_dep = dependency('absl_log_initialize')

protoc_src = ['google/protobuf/compiler/main.cc']
protoc = executable(
  'protoc',
  sources: protoc_src,
  include_directories: incdir,
  dependencies: [
    dependency('threads'),
    absl_log_initialize_dep,
    proto_includes_link_dep,
    protoc_dep,
  ],
  install: should_install,
)

if meson.is_cross_build()
  # one huge monolithyc blow, will be splitten up eventually for native grpc plug-ins
  cc_native = meson.get_compiler(
    'cpp',
    native: true,
  )
  protoc_native = executable(
    'protoc-native',
    sources: [
      libutf8_range_src,
      libprotobuf_lite_src,
      libprotobuf_src,
      libprotoc_src,
      protoc_src,
    ],
    c_args: ['-march=native'],
    native: true,
    include_directories: [
      incdir,
      include_directories('../third_party/utf8_range'),
    ],
    dependencies: [
      dependency(
        'absl_base',
        native: true,
        default_options: ['cpp_std=c++17'],
      ),
      dependency(
        'absl_cord',
        native: true,
      ),
      dependency(
        'absl_die_if_null',
        native: true,
      ),
      dependency(
        'absl_hash',
        native: true,
      ),
      dependency(
        'absl_log',
        native: true,
      ),
      dependency(
        'absl_log_internal_check_op',
        native: true,
      ),
      dependency(
        'absl_log_internal_message',
        native: true,
      ),
      dependency(
        'absl_numeric',
        native: true,
      ),
      dependency(
        'absl_raw_hash_set',
        native: true,
      ),
      dependency(
        'absl_status',
        native: true,
      ),
      dependency(
        'absl_statusor',
        native: true,
      ),
      dependency(
        'absl_str_format',
        native: true,
      ),
      dependency(
        'absl_strings',
        native: true,
      ),
      dependency(
        'absl_synchronization',
        native: true,
      ),
      dependency(
        'absl_string_view',
        native: true,
      ),
      dependency(
        'threads',
        native: true,
      ),
      cc_native.find_library(
        'dbghelp',
        required: build_machine.system() == 'windows',
      ),
      proto_includes_link_dep,
    ],
  )
else
  protoc_native = protoc
endif

meson.override_find_program('protoc', protoc_native)

protoc_gen = generator(
  protoc_native,
  output: ['@BASENAME@.pb.cc', '@BASENAME@.pb.h'],
  arguments: ['@EXTRA_ARGS@', '--cpp_out=@BUILD_DIR@', '@INPUT@'],
)

# may contain something extra, got with `find src -name "*.h"`
if should_install
  install_headers(
    'google/protobuf/any.h',
    'google/protobuf/any.pb.h',
    'google/protobuf/api.pb.h',
    'google/protobuf/arena.h',
    'google/protobuf/arena_align.h',
    'google/protobuf/arena_allocation_policy.h',
    'google/protobuf/arena_cleanup.h',
    'google/protobuf/arena_test_util.h',
    'google/protobuf/arenastring.h',
    'google/protobuf/arenaz_sampler.h',
    'google/protobuf/compiler/allowlists/allowlist.h',
    'google/protobuf/compiler/allowlists/allowlists.h',
    'google/protobuf/compiler/annotation_test_util.h',
    'google/protobuf/compiler/code_generator.h',
    'google/protobuf/compiler/command_line_interface.h',
    'google/protobuf/compiler/command_line_interface_tester.h',
    'google/protobuf/compiler/cpp/cpp_generator.h',
    'google/protobuf/compiler/cpp/enum.h',
    'google/protobuf/compiler/cpp/extension.h',
    'google/protobuf/compiler/cpp/field.h',
    'google/protobuf/compiler/cpp/field_generators/generators.h',
    'google/protobuf/compiler/cpp/file.h',
    'google/protobuf/compiler/cpp/generator.h',
    'google/protobuf/compiler/cpp/helpers.h',
    'google/protobuf/compiler/cpp/message.h',
    'google/protobuf/compiler/cpp/message_layout_helper.h',
    'google/protobuf/compiler/cpp/names.h',
    'google/protobuf/compiler/cpp/options.h',
    'google/protobuf/compiler/cpp/padding_optimizer.h',
    'google/protobuf/compiler/cpp/parse_function_generator.h',
    'google/protobuf/compiler/cpp/service.h',
    'google/protobuf/compiler/cpp/tracker.h',
    'google/protobuf/compiler/cpp/unittest.h',
    'google/protobuf/compiler/csharp/csharp_doc_comment.h',
    'google/protobuf/compiler/csharp/csharp_enum.h',
    'google/protobuf/compiler/csharp/csharp_enum_field.h',
    'google/protobuf/compiler/csharp/csharp_field_base.h',
    'google/protobuf/compiler/csharp/csharp_generator.h',
    'google/protobuf/compiler/csharp/csharp_helpers.h',
    'google/protobuf/compiler/csharp/csharp_map_field.h',
    'google/protobuf/compiler/csharp/csharp_message.h',
    'google/protobuf/compiler/csharp/csharp_message_field.h',
    'google/protobuf/compiler/csharp/csharp_options.h',
    'google/protobuf/compiler/csharp/csharp_primitive_field.h',
    'google/protobuf/compiler/csharp/csharp_reflection_class.h',
    'google/protobuf/compiler/csharp/csharp_repeated_enum_field.h',
    'google/protobuf/compiler/csharp/csharp_repeated_message_field.h',
    'google/protobuf/compiler/csharp/csharp_repeated_primitive_field.h',
    'google/protobuf/compiler/csharp/csharp_source_generator_base.h',
    'google/protobuf/compiler/csharp/csharp_wrapper_field.h',
    'google/protobuf/compiler/csharp/names.h',
    'google/protobuf/compiler/importer.h',
    'google/protobuf/compiler/java/context.h',
    'google/protobuf/compiler/java/doc_comment.h',
    'google/protobuf/compiler/java/enum.h',
    'google/protobuf/compiler/java/enum_field.h',
    'google/protobuf/compiler/java/enum_field_lite.h',
    'google/protobuf/compiler/java/enum_lite.h',
    'google/protobuf/compiler/java/extension.h',
    'google/protobuf/compiler/java/extension_lite.h',
    'google/protobuf/compiler/java/field.h',
    'google/protobuf/compiler/java/file.h',
    'google/protobuf/compiler/java/generator.h',
    'google/protobuf/compiler/java/generator_factory.h',
    'google/protobuf/compiler/java/helpers.h',
    'google/protobuf/compiler/java/java_features.pb.h',
    'google/protobuf/compiler/java/java_generator.h',
    'google/protobuf/compiler/java/kotlin_generator.h',
    'google/protobuf/compiler/java/map_field.h',
    'google/protobuf/compiler/java/map_field_lite.h',
    'google/protobuf/compiler/java/message.h',
    'google/protobuf/compiler/java/message_builder.h',
    'google/protobuf/compiler/java/message_builder_lite.h',
    'google/protobuf/compiler/java/message_field.h',
    'google/protobuf/compiler/java/message_field_lite.h',
    'google/protobuf/compiler/java/message_lite.h',
    'google/protobuf/compiler/java/message_serialization.h',
    'google/protobuf/compiler/java/name_resolver.h',
    'google/protobuf/compiler/java/names.h',
    'google/protobuf/compiler/java/options.h',
    'google/protobuf/compiler/java/primitive_field.h',
    'google/protobuf/compiler/java/primitive_field_lite.h',
    'google/protobuf/compiler/java/service.h',
    'google/protobuf/compiler/java/shared_code_generator.h',
    'google/protobuf/compiler/java/string_field.h',
    'google/protobuf/compiler/java/string_field_lite.h',
    'google/protobuf/compiler/mock_code_generator.h',
    'google/protobuf/compiler/objectivec/enum.h',
    'google/protobuf/compiler/objectivec/enum_field.h',
    'google/protobuf/compiler/objectivec/extension.h',
    'google/protobuf/compiler/objectivec/field.h',
    'google/protobuf/compiler/objectivec/file.h',
    'google/protobuf/compiler/objectivec/generator.h',
    'google/protobuf/compiler/objectivec/helpers.h',
    'google/protobuf/compiler/objectivec/import_writer.h',
    'google/protobuf/compiler/objectivec/line_consumer.h',
    'google/protobuf/compiler/objectivec/map_field.h',
    'google/protobuf/compiler/objectivec/message.h',
    'google/protobuf/compiler/objectivec/message_field.h',
    'google/protobuf/compiler/objectivec/names.h',
    'google/protobuf/compiler/objectivec/nsobject_methods.h',
    'google/protobuf/compiler/objectivec/oneof.h',
    'google/protobuf/compiler/objectivec/options.h',
    'google/protobuf/compiler/objectivec/primitive_field.h',
    'google/protobuf/compiler/objectivec/text_format_decode_data.h',
    'google/protobuf/compiler/package_info.h',
    'google/protobuf/compiler/parser.h',
    'google/protobuf/compiler/php/names.h',
    'google/protobuf/compiler/php/php_generator.h',
    'google/protobuf/compiler/plugin.h',
    'google/protobuf/compiler/plugin.pb.h',
    'google/protobuf/compiler/python/generator.h',
    'google/protobuf/compiler/python/helpers.h',
    'google/protobuf/compiler/python/pyi_generator.h',
    'google/protobuf/compiler/python/python_generator.h',
    'google/protobuf/compiler/retention.h',
    'google/protobuf/compiler/ruby/ruby_generator.h',
    'google/protobuf/compiler/rust/accessors/accessor_generator.h',
    'google/protobuf/compiler/rust/accessors/accessors.h',
    'google/protobuf/compiler/rust/context.h',
    'google/protobuf/compiler/rust/generator.h',
    'google/protobuf/compiler/rust/message.h',
    'google/protobuf/compiler/rust/naming.h',
    'google/protobuf/compiler/rust/oneof.h',
    'google/protobuf/compiler/rust/relative_path.h',
    'google/protobuf/compiler/scc.h',
    'google/protobuf/compiler/subprocess.h',
    'google/protobuf/compiler/versions.h',
    'google/protobuf/compiler/versions_suffix.h',
    'google/protobuf/compiler/zip_writer.h',
    'google/protobuf/cpp_edition_defaults.h',
    'google/protobuf/cpp_features.pb.h',
    'google/protobuf/descriptor.h',
    'google/protobuf/descriptor.pb.h',
    'google/protobuf/descriptor_database.h',
    'google/protobuf/descriptor_legacy.h',
    'google/protobuf/descriptor_visitor.h',
    'google/protobuf/duration.pb.h',
    'google/protobuf/dynamic_message.h',
    'google/protobuf/empty.pb.h',
    'google/protobuf/endian.h',
    'google/protobuf/explicitly_constructed.h',
    'google/protobuf/extension_set.h',
    'google/protobuf/extension_set_inl.h',
    'google/protobuf/feature_resolver.h',
    'google/protobuf/field_access_listener.h',
    'google/protobuf/field_mask.pb.h',
    'google/protobuf/generated_enum_reflection.h',
    'google/protobuf/generated_enum_util.h',
    'google/protobuf/generated_message_bases.h',
    'google/protobuf/generated_message_reflection.h',
    'google/protobuf/generated_message_tctable_decl.h',
    'google/protobuf/generated_message_tctable_gen.h',
    'google/protobuf/generated_message_tctable_impl.h',
    'google/protobuf/generated_message_util.h',
    'google/protobuf/has_bits.h',
    'google/protobuf/implicit_weak_message.h',
    'google/protobuf/inlined_string_field.h',
    'google/protobuf/internal_message_util.h',
    'google/protobuf/internal_visibility.h',
    'google/protobuf/internal_visibility_for_testing.h',
    'google/protobuf/io/coded_stream.h',
    'google/protobuf/io/gzip_stream.h',
    'google/protobuf/io/io_win32.h',
    'google/protobuf/io/package_info.h',
    'google/protobuf/io/printer.h',
    'google/protobuf/io/strtod.h',
    'google/protobuf/io/test_zero_copy_stream.h',
    'google/protobuf/io/tokenizer.h',
    'google/protobuf/io/zero_copy_sink.h',
    'google/protobuf/io/zero_copy_stream.h',
    'google/protobuf/io/zero_copy_stream_impl.h',
    'google/protobuf/io/zero_copy_stream_impl_lite.h',
    'google/protobuf/json/internal/descriptor_traits.h',
    'google/protobuf/json/internal/lexer.h',
    'google/protobuf/json/internal/message_path.h',
    'google/protobuf/json/internal/parser.h',
    'google/protobuf/json/internal/parser_traits.h',
    'google/protobuf/json/internal/unparser.h',
    'google/protobuf/json/internal/unparser_traits.h',
    'google/protobuf/json/internal/untyped_message.h',
    'google/protobuf/json/internal/writer.h',
    'google/protobuf/json/internal/zero_copy_buffered_stream.h',
    'google/protobuf/json/json.h',
    'google/protobuf/map.h',
    'google/protobuf/map_entry.h',
    'google/protobuf/map_field.h',
    'google/protobuf/map_field_inl.h',
    'google/protobuf/map_field_lite.h',
    'google/protobuf/map_lite_test_util.h',
    'google/protobuf/map_test_util.h',
    'google/protobuf/map_test_util_impl.h',
    'google/protobuf/map_type_handler.h',
    'google/protobuf/message.h',
    'google/protobuf/message_lite.h',
    'google/protobuf/metadata.h',
    'google/protobuf/metadata_lite.h',
    'google/protobuf/package_info.h',
    'google/protobuf/parse_context.h',
    'google/protobuf/port.h',
    'google/protobuf/raw_ptr.h',
    'google/protobuf/reflection.h',
    'google/protobuf/reflection_internal.h',
    'google/protobuf/reflection_mode.h',
    'google/protobuf/reflection_ops.h',
    'google/protobuf/reflection_tester.h',
    'google/protobuf/repeated_field.h',
    'google/protobuf/repeated_ptr_field.h',
    'google/protobuf/serial_arena.h',
    'google/protobuf/service.h',
    'google/protobuf/source_context.pb.h',
    'google/protobuf/string_block.h',
    'google/protobuf/string_member_robber.h',
    'google/protobuf/struct.pb.h',
    'google/protobuf/stubs/callback.h',
    'google/protobuf/stubs/common.h',
    'google/protobuf/stubs/platform_macros.h',
    'google/protobuf/stubs/port.h',
    'google/protobuf/stubs/status_macros.h',
    'google/protobuf/test_textproto.h',
    'google/protobuf/test_util.h',
    'google/protobuf/test_util2.h',
    'google/protobuf/test_util_lite.h',
    'google/protobuf/testing/file.h',
    'google/protobuf/testing/googletest.h',
    'google/protobuf/text_format.h',
    'google/protobuf/thread_safe_arena.h',
    'google/protobuf/timestamp.pb.h',
    'google/protobuf/type.pb.h',
    'google/protobuf/unknown_field_set.h',
    'google/protobuf/util/delimited_message_util.h',
    'google/protobuf/util/field_comparator.h',
    'google/protobuf/util/field_mask_util.h',
    'google/protobuf/util/json_util.h',
    'google/protobuf/util/message_differencer.h',
    'google/protobuf/util/package_info.h',
    'google/protobuf/util/time_util.h',
    'google/protobuf/util/type_resolver.h',
    'google/protobuf/util/type_resolver_util.h',
    'google/protobuf/varint_shuffle.h',
    'google/protobuf/wire_format.h',
    'google/protobuf/wire_format_lite.h',
    'google/protobuf/wrappers.pb.h',
    preserve_path: true,
  )
endif
